package com.mp3player.xml;

import com.mp3player.model.Mp3Info;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.List;

/**
 * Created by Cuckoo322 on 3/19/2017.
 */

public class Mp3ListContentHandler extends DefaultHandler {
    private List<Mp3Info> mp3InfoList = null;
    private Mp3Info mp3Info = null;
    private String tagName = null;

    public Mp3ListContentHandler(List<Mp3Info> mp3InfoList) {
        this.mp3InfoList = mp3InfoList;
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        this.tagName = localName;
        if ("resource".equals(this.tagName)) {
            mp3Info = new Mp3Info();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("resource".equals(qName)) {
            mp3InfoList.add(mp3Info);
        }
        this.tagName = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if ("id".equals(this.tagName)) {
            mp3Info.setId(new String(ch, start, length));
        } else if ("mp3.name".equals(this.tagName)) {
            mp3Info.setMp3Name(new String(ch, start, length));
        } else if ("mp3.size".equals(this.tagName)) {
            mp3Info.setMp3Size(new String(ch, start, length));
        } else if ("lrc.name".equals(this.tagName)) {
            mp3Info.setLrcName(new String(ch, start, length));
        } else if ("lrc.size".equals(this.tagName)) {
            mp3Info.setLrcSize(new String(ch, start, length));
        }
    }
}
